package com.paomi.client.fragment.card;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.paomi.client.GlideApp;
import com.paomi.client.R;
import com.paomi.client.activity.person.model.MyCardBean;
import com.paomi.client.base.BaseLCEFragment;
import com.paomi.client.fragment.card.presenter.CardBeOverduePre;
import com.paomi.client.utils.UIUtils;
import com.paomi.client.utils.Util;

import java.text.SimpleDateFormat;
import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;

public class CardBeOverdueFragment extends BaseLCEFragment<MyCardBean.Data, CardBeOverduePre> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.ptr_refresh)
    PtrClassicFrameLayout ptrRefresh;
    private int page=1;


    @NonNull
    @Override
    protected int getFragmentLayoutId() {
        return R.layout.layout_only_rec;
    }

    @NonNull
    @Override
    protected RecyclerView createRecycler() {
        return recyclerView;
    }

    @Override
    protected int getItemLayout() {
        return R.layout.item_my_card;
    }
    @Override
    protected void initEvent() {
        super.initEvent();
        createRecycler().setPadding(0,0,0, UIUtils.dp2px(getActivity(),80f));
        setPtrFrameLayout(ptrRefresh);
        getPresenter().getLoadData(getActivity(),page);
    }


    @Override
    protected void initEmpty(TextView empty) {
        empty.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.img_k_kaquan,0,0);
        empty.setText(getResources().getString(R.string.card_list_null));

    }

    @SuppressLint("NewApi")
    @Override
    protected void convert(BaseViewHolder helper, MyCardBean.Data item) {
        RelativeLayout rl_ls = helper.itemView.findViewById(R.id.rl_ls);
        RelativeLayout rl_lx = helper.itemView.findViewById(R.id.rl_lx);
        LinearLayout rl_rs = helper.itemView.findViewById(R.id.rl_rs);
        RelativeLayout rl_rx = helper.itemView.findViewById(R.id.rl_rx);
        int width = Util.getDisplay(getActivity()).widthPixels - Util.dip2px(getActivity(), 30);
        Util.setWidthAndHeight(rl_ls, (int) (width * 0.27), -1);
        Util.setWidthAndHeight(rl_lx, (int) (width * 0.27), -1);
        Util.setWidthAndHeight(rl_rs, (int) (width * 0.73), -1);
        Util.setWidthAndHeight(rl_rx, (int) (width * 0.73), -1);
        rl_ls.setBackground(getActivity().getResources().getDrawable(R.mipmap.bg_card_ls_h));
        rl_lx.setBackground(getActivity().getResources().getDrawable(R.mipmap.bg_card_lx_h));
        ImageView iv = helper.getView(R.id.iv_img);
        helper.setTextColor(R.id.click,getResources().getColor(R.color.text_gray_99));
        helper.setBackgroundRes(R.id.click,R.drawable.btn_gray_out);
        helper.setText(R.id.click,R.string.be_overdue);
        if (item.getType() == 1) {
            GlideApp.with(getActivity()).asBitmap().load(R.mipmap.img_card_you_h).into(iv);
        } else {
            GlideApp.with(getActivity()).asBitmap().load(R.mipmap.img_card_piao_h).into(iv);
        }
        helper.setText(R.id.tv_card_name, getResources().getString(R.string.card_name) + item.getName());
        helper.setText(R.id.tv_msg, "满"+item.getEnableAmount()+"使用");
        helper.setTextColor(R.id.tv_msg, getResources().getColor(R.color.white));
        helper.setText(R.id.tv_card_assname, getResources().getString(R.string.card_assName) + item.getAssName());
        helper.setText(R.id.tv_card_data, getResources().getString(R.string.card_data)
                + new SimpleDateFormat("yyyy-MM-dd").format(item.getExpireDdate()*1000));
    }

    @Override
    protected void onItemClick(View view, MyCardBean.Data item) {

    }


    public void aaa(){
        Util.toast(getActivity(),"11111");
    }

    @Override
    public CardBeOverduePre createPresenter() {
        return new CardBeOverduePre();
    }

    @Override
    public void setData(List<MyCardBean.Data> data, boolean pullToRefresh) {
        super.setData(data, pullToRefresh);
        if(data.size()<=9){//移除FooterView
            getmAdapterWrapper().setLoadItemVisibility(false);
        }
        getmLoadMoreHelper().setLoadMoreFinish();
        getmAdapterWrapper().notifyDataSetChanged();
    }

    @Override
    public void loadData(boolean pullToRefresh) {
        super.loadData(pullToRefresh);
        //下拉刷新
        page=1;
        getPresenter().getLoadData(getActivity(),page);
    }

    @Override
    public void onLoad() {
        super.onLoad();
        //加载更多
        page++;
        getPresenter().getLoadData(getActivity(),page);
    }
}
